<script lang="ts" setup name="XtxCity">


// 选择的城市结果类型
export type CityResult = {
  provinceCode: string
  provinceName: string
  cityCode: string
  cityName: string
  countyCode: string
  countyName: string
}
import { ref, watch } from 'vue'
import axios from 'axios';
import { onClickOutside } from '@vueuse/core';
// 接收父组件传递过来的数据ts的defineProps
defineProps<{
  userAddress?:string
}>()

const emit = defineEmits<{
  (e: 'changeCity', value: CityResult): void
}>()

const active = ref(false)// 控制省市区显示隐藏
const toggle = () => {
  active.value = !active.value
}
// 处理点击在外面时关闭弹框
const target = ref(null)
onClickOutside(target, (event) => {
  active.value = false
})

type AreaList = {
  code: string
  level: number
  name: string
  areaList: AreaList[]
}
// 存储的是获取省市区数据
const cityList = ref<AreaList[]>([])
// 缓存输出
const cacheList = ref<AreaList[]>([])
const getCityList = async () => {
  const res = await axios.get<AreaList[]>(
    'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
  )
  cityList.value = res.data
  // 同时将数据存储到缓存中
  cacheList.value = res.data
}
getCityList()

// 存储用户选择完的信息
const changeResult = ref({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: ''
})
const selectCity = (city: AreaList) => {
  if (city.level === 0) {
    // 省
    changeResult.value.provinceName = city.name
    changeResult.value.provinceCode = city.code
    cityList.value = city.areaList
  }
  if (city.level === 1) {
    // 市
    changeResult.value.cityName = city.name
    changeResult.value.cityCode = city.code
    cityList.value = city.areaList
  }
  if (city.level === 2) {
    // 县（区）
    changeResult.value.countyName = city.name
    changeResult.value.countyCode = city.code
    // 表示用户选完了
    emit('changeCity',changeResult.value)
    // 关闭弹窗
    active.value = false
  }
}
// 监听弹框的关闭，只要弹框关了，就需要重置数据
// 这里一进入页面不需要立即执行一次，正常用watch（watchEffect 一般就是用于处理路由缓存的问题）
// val 变化后的值 侦听新增val 新值false 就恢复数据
watch(active, (val) => {
  if (!val) {
    // 当关闭active的时候，需要恢复数据
    cityList.value = cacheList.value
  }
})


</script>
<template>

  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{ active : active}">
      <span class="value" v-if="userAddress">{{ userAddress }}</span>
      <span v-else class="placeholder" >请选择配送地址</span>
      
      <i class="iconfont icon-angle-down"></i>
    </div>

    <div class="option" v-show="active">
      <span class="ellipsis" v-for="item in cityList" :key="item.code" @click="selectCity(item)">{{ item.name }}</span>
    </div>

  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;

  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;

    &.active {
      background: #fff;
    }

    .placeholder {
      color: #999;
    }

    .value {
      color: #666;
      font-size: 12px;
    }

    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }

  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;

    >span {
      width: 130px;
      text-align: center;

      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;

      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>